<?php
namespace App\Repositories\Extended;
use Cache,Config;
use Zizaco\Entrust\EntrustRole as OrgEntrustRole;
use Zizaco\Entrust\Contracts\EntrustRoleInterface;

class EntrustRole extends OrgEntrustRole implements EntrustRoleInterface
{
    //Big block of caching functionality.
    public function cachedPermissions()
    {
        $rolePrimaryKey = $this->primaryKey;
        $cacheKey = 'entrust_permissions_for_role_'.$this->$rolePrimaryKey;
        return Cache::store('array')->tags(Config::get('entrust.permission_role_table'))->remember($cacheKey, Config::get('cache.ttl'), function () {
            return $this->perms()->get();
        });
    }
    public function save(array $options = [])
    {   //both inserts and updates
        $result = parent::save($options);
        Cache::store('array')->tags(Config::get('entrust.permission_role_table'))->flush();
        return $result;
    }
    public function delete(array $options = [])
    {   //soft or hard
        $result = parent::delete($options);
        Cache::store('array')->tags(Config::get('entrust.permission_role_table'))->flush();
        return $result;
    }
    public function restore()
    {   //soft delete undo's
        $result = parent::restore();
        Cache::store('array')->tags(Config::get('entrust.permission_role_table'))->flush();
        return $result;
    }
}